package com.jiyi.activity.datainput.sm;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jiyi.activity.WebActivity;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;
import com.swift.utils.WebViewBundle;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SMDataInputActivity extends ActivityBase {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;
	@BindView(R.id.tvTip)
	TextView tvTip;

	String build_no;
	final int BASE = 1;
	//1 读  2 写
	int type = 2;
	//是否可以查看报表
	boolean canViewReport = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_data_input);
		ButterKnife.bind(this);
		init();
	}

	/**
	* @desc 
	* @author hym
	* @createDate 2018/1/17 15:48
	*/
	private void init() {
		getIntentData();
		setTop();
	}

	private void getIntentData() {
		type = getIntent().getIntExtra("type",1);		
		build_no = getIntent().getStringExtra("build_no");
		canViewReport = getIntent().getBooleanExtra("canViewReport", false);
	}

	private void setTop() {
		if (type == 1) {
			tvTitle.setText("项目查看");
			tvTip.setVisibility(View.GONE);
		}
		else {
			tvTitle.setText("项目新增");
		}
		
		if (canViewReport) {
			tvSearch.setCompoundDrawables(null, null, null, null);
			tvSearch.setText("查看报表");
			tvSearch.setVisibility(View.VISIBLE);
		}
		else {
			tvSearch.setVisibility(View.GONE);
		}
	}

	@OnClick({R.id.tvBack,R.id.tvSearch,R.id.cvBase,R.id.cvDetail,R.id.cvXZXK,R.id.cvAQTJ,R.id.cvGLFM })
	public void onClick(View view) {
		Intent intent;
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		case R.id.cvBase:
			// 建筑基础属性
			if (type == 1) {
				intent = new Intent(this, BuildBaseInfoReadActivity.class);
			}
			else {
				intent = new Intent(this,BuildBaseInfoActivity.class);
			}


			intent.putExtra("build_no", build_no);
			startActivityForResult(intent, BASE);
			break;
			case R.id.cvDetail:
				if (!isBase()) {
					return;
				}
				// 建筑详细情况
				if (type == 1) {
					intent = new Intent(this, BuildDetailInfoReadActivity.class);
				}
				else {
					intent = new Intent(this,BuildDetailInfoActivity.class);
				}

				intent.putExtra("build_no",build_no);
				startActivity(intent);
				break;
			case R.id.cvXZXK:
				if (!isBase()) {
					return;
				}

				//建筑场所消防行政许可
				if (type == 1) {
					intent = new Intent(this, BuildSpaceLawReadActiviy.class);
				}
				else {
					intent = new Intent(this,BuildSpaceLawActiviy.class);
				}
				intent.putExtra("build_no",build_no);
				startActivity(intent);

				break;
			case R.id.cvAQTJ:
				if (!isBase()) {
					return;
				}
				//建筑场所消防安全条件
				intent = new Intent(this, BuildSpaceSafeActivity.class);
				intent.putExtra("build_no",build_no);
				intent.putExtra("type",type);
				startActivity(intent);
				break;
			case R.id.cvGLFM:
				if (!isBase()) {
					return;
				}
				//消防安全管理方面
				if (type == 1) {
					intent = new Intent(this, BuildSpaceManageReadActivity.class);
				}
				else {
					intent = new Intent(this,BuildSpaceManageActivity.class);
				}

				intent.putExtra("build_no",build_no);
				startActivity(intent);
				break;

		case R.id.tvSearch:
			intent = new Intent(this,WebActivity.class);
			intent.putExtra("title", "报表");
			intent.putExtra("rightClickTitle", "下载");
			intent.putExtra("WebRightClickCallBack", WebActivity.RightClickEvent.downLoadDataInput);
			intent.putExtra("isHideTitle", false);
			WebViewBundle mWebViewBundle = new WebViewBundle();
			mWebViewBundle.addKeyMap("access_token", UserInfoUtil.getCurrentUserAccessToken());
			mWebViewBundle.addKeyMap("build_id", build_no);
			mWebViewBundle.addKeyMap("postUrl", AppConfig.TEXT_HOST+"/Api/FireSafetyNew/export");
			Bundle bundle = mWebViewBundle.build();
			intent.putExtras(bundle);

			intent.putExtra("url", AppConfig.TEXT_HOST+"/Api/FireSafetyNew/pro_report/access_token/"+ UserInfoUtil.getCurrentUserAccessToken()+"/build_id/"+build_no);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == BASE) {
			if (resultCode == RESULT_OK) {
				build_no = data.getStringExtra("build_no");
			}
		}
	}

	public boolean isBase() {
		if (TextUtils.isEmpty(build_no) && type == 2) {
			ToastUtil.getInstances().showMsg("请先完成基础信息");
			return false;
		}
		return true;
	}

}
